package com.fulihui.wgj.admin.integration.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.conv.BeanConv;
import com.fulihui.common.result.rpc.BaseResult;
import com.fulihui.common.result.rpc.SimpleResult;
import com.fulihui.common.result.web.Result;
import com.fulihui.common.util.DateUtils;
import com.fulihui.common.util.RpcResultCheckUtil;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateECRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.SendTemplateGameRequest;
import com.fulihui.wgj.admin.integration.SendTemplateMsgServiceClient;
import com.fulihui.wgj.facade.request.game.CalcSendCountGameContainRequest;
import com.fulihui.wgj.facade.request.game.CalcSendCountGameEliminateRequest;
import com.fulihui.wgj.facade.request.template.CalcSendCountRequest;
import com.fulihui.wgj.facade.request.youzan.TradeOrderQueryRequest;
import com.fulihui.wgj.facade.service.template.SendTemplateMsgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.util.Date;

/**
 * @author: Levon
 * @date: 2018-07-30 14:16
 */
@Service
public class SendTemplateMsgServiceClientImpl implements SendTemplateMsgServiceClient {

    @Reference(version = "1.0.0")
    private SendTemplateMsgService sendTemplateMsgService;

    @Override
    public Result<Boolean> sendEcTemplate(SendTemplateECRequest request) {
        CalcSendCountRequest sendCountRequest = BeanConv.copy(request, CalcSendCountRequest.class);
        if (!CollectionUtils.isEmpty(request.getYzShopid()) && request.isOpenYouZan()) {
            TradeOrderQueryRequest yzFilter = buildTradeOrderQuery(request);
            sendCountRequest.setYzFilter(yzFilter);
        }
        sendCountRequest.setSex(StringUtils.isNotBlank(request.getSex()) ? Long.valueOf(request.getSex()) : null);
        try {
            sendCountRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
            sendCountRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            sendCountRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BaseResult baseResult = sendTemplateMsgService.sendTemplateMsg(sendCountRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<Boolean> editSendEcTemplate(SendTemplateECRequest request) {
        CalcSendCountRequest sendCountRequest = BeanConv.copy(request, CalcSendCountRequest.class);
        sendCountRequest.setRecordId(request.getId());
        if (!CollectionUtils.isEmpty(request.getYzShopid()) && request.isOpenYouZan()) {
            TradeOrderQueryRequest yzFilter = buildTradeOrderQuery(request);
            sendCountRequest.setYzFilter(yzFilter);
        }
        sendCountRequest.setSex(StringUtils.isNotBlank(request.getSex()) ? Long.valueOf(request.getSex()) : null);
        try {
            sendCountRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
            sendCountRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            sendCountRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BaseResult baseResult = sendTemplateMsgService.sendTemplateMsgEdit(sendCountRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }


    @Override
    public Result<String> countSendUser(SendTemplateECRequest request) {
        CalcSendCountRequest sendCountRequest = BeanConv.copy(request, CalcSendCountRequest.class);
        if (!CollectionUtils.isEmpty(request.getYzShopid()) && request.isOpenYouZan()) {
            TradeOrderQueryRequest yzFilter = buildTradeOrderQuery(request);
            sendCountRequest.setYzFilter(yzFilter);
        }
        sendCountRequest.setSex(StringUtils.isNotBlank(request.getSex()) ? Long.valueOf(request.getSex()) : null);
        try {
            sendCountRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
            sendCountRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            sendCountRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleResult<Long> simpleResult = sendTemplateMsgService.countSendUser(sendCountRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            return JsonResultBuilder.succ(String.valueOf(simpleResult.getData()));
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    @Override
    public Result<Boolean> sendGameTemplate(SendTemplateGameRequest request) {
        CalcSendCountRequest sendCountRequest = BeanConv.copy(request, CalcSendCountRequest.class);
        sendCountRequest.setContainRequest(BeanConv.copy(request.getContainRequest(),
                CalcSendCountGameContainRequest.class));
        sendCountRequest.setEliminateRequest(BeanConv.copy(request.getEliminateRequest(),
                CalcSendCountGameEliminateRequest.class));
        sendCountRequest.setSex(StringUtils.isNotBlank(request.getSex()) ? Long.valueOf(request.getSex()) : null);
        try {
            sendCountRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
            sendCountRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            sendCountRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BaseResult baseResult = sendTemplateMsgService.sendTemplateMsg4Game(sendCountRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<Boolean> editSendGameTemplate(SendTemplateGameRequest request) {
        CalcSendCountRequest sendCountRequest = BeanConv.copy(request, CalcSendCountRequest.class);
        sendCountRequest.setRecordId(request.getId());
        sendCountRequest.setContainRequest(BeanConv.copy(request.getContainRequest(),
                CalcSendCountGameContainRequest.class));
        sendCountRequest.setEliminateRequest(BeanConv.copy(request.getEliminateRequest(),
                CalcSendCountGameEliminateRequest.class));
        sendCountRequest.setSex(StringUtils.isNotBlank(request.getSex()) ? Long.valueOf(request.getSex()) : null);
        try {
            sendCountRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
            sendCountRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            sendCountRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BaseResult baseResult = sendTemplateMsgService.sendTemplateMsgEdit4Game(sendCountRequest);
        if (RpcResultCheckUtil.isSuccess(baseResult)) {
            return JsonResultBuilder.succ(true);
        }
        return JsonResultBuilder.fail(baseResult.getErrcode(), baseResult.getErrmsg());
    }

    @Override
    public Result<String> countSendGameUser(SendTemplateGameRequest request) {
        CalcSendCountRequest sendCountRequest = BeanConv.copy(request, CalcSendCountRequest.class);
        sendCountRequest.setContainRequest(BeanConv.copy(request.getContainRequest(),
                CalcSendCountGameContainRequest.class));
        sendCountRequest.setEliminateRequest(BeanConv.copy(request.getEliminateRequest(),
                CalcSendCountGameEliminateRequest.class));
        sendCountRequest.setSex(StringUtils.isNotBlank(request.getSex()) ? Long.valueOf(request.getSex()) : null);
        try {
            sendCountRequest.setJobTime(DateUtils.parseNewFormat(request.getJobTime()));
            sendCountRequest.setSubEndTime(DateUtils.parseNewFormat(request.getSubEndTime()));
            sendCountRequest.setSubStartTime(DateUtils.parseNewFormat(request.getSubStartTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleResult<Long> simpleResult = sendTemplateMsgService.countSendUser4Game(sendCountRequest);
        if (RpcResultCheckUtil.isSuccess(simpleResult)) {
            return JsonResultBuilder.succ(String.valueOf(simpleResult.getData()));
        }
        return JsonResultBuilder.fail(simpleResult.getErrcode(), simpleResult.getErrmsg());
    }

    /**
     * 组装有赞过滤条件
     *
     * @param request
     * @return
     */
    private TradeOrderQueryRequest buildTradeOrderQuery(SendTemplateECRequest request) {

        TradeOrderQueryRequest tradeOrderRequest = new TradeOrderQueryRequest();
        tradeOrderRequest.setCommodityIds(request.getYzShopid());

        String yzStartTime = request.getYzStartTime();
        if (!StringUtils.isEmpty(yzStartTime)) {
            Date yzStartDate = null;
            try {
                yzStartDate = DateUtils.parseNewFormat(yzStartTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            yzStartDate.setHours(0);
            tradeOrderRequest.setStartTime(yzStartDate.getTime());
        }

        String yzEndTime = request.getYzEndTime();
        if (!StringUtils.isEmpty(yzEndTime)) {
            Date yzEndDate = null;
            try {
                yzEndDate = DateUtils.parseNewFormat(yzEndTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            yzEndDate.setHours(0);
            tradeOrderRequest.setEndTime(yzEndDate.getTime() + 86399000);
        }
        tradeOrderRequest.setOrderState(request.getYzOrderState());
        return tradeOrderRequest;
    }
}
